package com.wd.servlet;

import cn.hutool.core.text.StrSpliter;
import com.wd.entity.User;
import com.wd.service.UserService;
import com.wd.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/11/8 14:35
 * @Description: ${description}
 * @Version: 1.0
 */
@WebServlet(urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            boolean flag=true;
            HttpSession hs=request.getSession();
            String op = request.getParameter("op");
            List<User> users = userService.selAllUser();
            request.setAttribute("userList",users);
            if(op!=null&&!"null".equals(op)){
                flag=false;
                switch (op) {
                    case "updUser":
                        updUser(request,response,hs);
                        break;
                    case "delUser":
                        delUser(request,response,hs);
                        break;
                    default:
                        PrintWriter out = response.getWriter();
                        out.print("无效的指令");
                        out.close();
                }
            }
            if(flag) {
                request.getRequestDispatcher("user.jsp").forward(request, response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void updUser(HttpServletRequest request, HttpServletResponse response,HttpSession hs)
            throws Exception {
        hs.setAttribute("userId",request.getParameter("userId"));
        hs.setAttribute("op1","selUserById");
        response.sendRedirect("EditUserServlet");
    }

    protected void delUser(HttpServletRequest request, HttpServletResponse response,HttpSession hs)
            throws Exception {
        String userId = request.getParameter("userId");
        List<String> list = StrSpliter.splitTrim(userId, ',', true);
        for(String a:list){
            int i = userService.delUser(Integer.parseInt(a));
            if(i!=1){
                hs.setAttribute("MSG","201");
                break;
            }
        }
        response.sendRedirect("UserServlet");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TagServlet.isIllegalLogin(request,response);
        HttpSession session=request.getSession();
        String placeLogin =(String) session.getAttribute("illegalLogin");
        if(placeLogin.equals("请登录")){
            return;
        }
        doPost(request,response);
    }

}
